package com.yanqu.road.server.cmd.activity.dafuweng.zaoyao;

import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.dafuweng.DFWUserData;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.pb.DaFuWengPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_DA_FU_WENG_ZAO_YAO_FROM_CROSS, desc = "处理来自跨服的造谣")
public class DaFuWengZaoYaoFromCrossCmd implements Command {

    int activityId;
    long doServerId;
    long doUserId;
    String unionUid;
    MonopolyProto.MonopolyPlayerMsg doPlayer;
    int type;
    private int recordId;

    MonopolyProto.ZaoYaoSceneMsg.Builder sceneMsg = MonopolyProto.ZaoYaoSceneMsg.newBuilder();

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        MonopolyProto.ZaoYaoFromCrossReqMsg reqMsg = MonopolyProto.ZaoYaoFromCrossReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        doServerId = reqMsg.getDoServerId();
        doUserId = reqMsg.getDoUserId();
        unionUid = reqMsg.getUnionUid();
        doPlayer = reqMsg.getDoPlayer();
        type = reqMsg.getType();
        recordId = reqMsg.getRecordId();
        DFWConfig config = DaFuWengMgr.getDfwConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo()) || config.getActivityInfo().getActivityId() != activityId) {
            returnZaoYao(GameErrorCode.E_ACTIVITY_DFW_NO_IN_TIME);
            return;
        }
        long zaoyaoServerId = reqMsg.getZaoyaoServerId();
        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
        CrossUnionMgr.getLogger().info("unionInfo : {}",unionInfo);
        if (unionInfo == null) {
            unionInfo = CrossUnionMgr.getDeleteUnionInfoMap().get(unionUid);
            CrossUnionMgr.getLogger().info("unionInfo : {}",unionInfo);

            int result = DaFuWengMgr.zaoYaoFromCross(unionUid, activityId, doPlayer, sceneMsg);
            if (unionInfo == null) {
                return;
            }
            if (result != 0 && unionInfo.getServerId() == GameServer.getInstance().getServerId()) {
                returnZaoYao(result);
            }
        }else {
            Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
            CrossUnionMember masterMember = unionMemberMap.get(unionInfo.getMasterUserId());
            if (masterMember == null) {
                List<CrossUnionMember> memberList = new ArrayList<>(unionMemberMap.values());
                masterMember = memberList.get(0);
            }
            CrossUnionMgr.getLogger().info("master : {}",masterMember.getServerId());
            int result = DaFuWengMgr.zaoYaoFromCross(unionUid, activityId, doPlayer, sceneMsg);
            if (masterMember.getServerId() == GameServer.getInstance().getServerId()) {
                // 获取会长数据（给造谣者展示）
                DFWConfig dfwConfig = DaFuWengMgr.getDfwConfig();
                DFWUserData userData = DaFuWengMgr.getUserData(dfwConfig.getActivityInfo().getActivityId(), masterMember.getUserId());
                MonopolyProto.UnionMemberDollTemp.Builder masterDollData = DaFuWengPb.parseUnionMemberDollTemp(userData, masterMember.getUserBaseInfo());
                // 设置返回值
                sceneMsg.setOwnerData(masterDollData);
                List<Integer> addition = DaFuWengMgr.getUnionSkillAddition(dfwConfig, unionUid);
                sceneMsg.setUnionAddition(addition.get(0));
                returnZaoYao(result);
            }
        }
    }

    /**
     * 返回造谣
     *
     * @param
     */
    public void returnZaoYao(int result) {
        MonopolyProto.ZaoYaoReturnCrossReqMsg.Builder builder = MonopolyProto.ZaoYaoReturnCrossReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setDoServerId(doServerId);
        builder.setDoUserId(doUserId);
        builder.setType(type);
        builder.setResult(result);
        if (result == 0) {
            builder.setSceneData(sceneMsg);
        }
        builder.setRecordId(recordId);
        builder.setUnionUid(unionUid);

        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, doUserId);
        GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_DA_FU_WENG_HANDLE_ZAO_YAO_RETURN, builder, 0, activityId, groupId);
    }

}
